package nl.vu.few.ibivu.biomodels.server;

import nl.vu.few.ibivu.biomodels.shared.NotLoggedInException;
import nl.vu.few.ibivu.biomodels.shared.PetrifierModelExecution;
import nl.vu.few.ibivu.biomodels.shared.UserProfile;
import nl.vu.few.ibivu.biomodels.shared.Model;
import nl.vu.few.ibivu.biomodels.shared.uniprot.UniprotEntity;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.helper.DAOBase;

public class DAO extends DAOBase {
	
	static {
		ObjectifyService.register(Model.class);
		ObjectifyService.register(UserProfile.class);
		ObjectifyService.register(UniprotEntity.class);
		ObjectifyService.register(PetrifierModelExecution.class);
	}

	/** Your DAO can have your own useful methods */
	public UserProfile getOrCreateUserProfile() throws NotLoggedInException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        if (user == null)
        	throw new NotLoggedInException("Login required");
        UserProfile userProfile = ofy().query(UserProfile.class).filter("user", user).get();
        if (userProfile == null) {
        	userProfile = new UserProfile();
        	userProfile.setUser(user);
        	ofy().put(userProfile);
        }
        return userProfile;
	}
}
